package com.gack.business.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gack.business.model.BackgroundPicture;
import com.gack.business.model.FriendShip;
import com.gack.business.model.OperatorManage;
import com.gack.business.model.RequestModel;
import com.gack.business.repository.BackgroundPictureRepository;
import com.gack.business.repository.FriendShipRepository;
import com.gack.business.repository.ManagerInfoRepository;
import com.gack.helper.common.abstractobj.Result;

@Service
public class OperatorManagerService implements OperatorManagerServiceInterface {
	@Autowired
	private BackgroundPictureRepository backgroundPictureRepository;
	@Autowired
	private ManagerInfoRepository managerInfoRepository;
	@Autowired
	private FriendShipRepository friendShipRepository;

	
	@Autowired
	private BackLogServiceInterface backLogServiceInterface;

	@Override
	public Result saveBackgroundPicture(String uid, String picture) {
		backgroundPictureRepository.deleteAll();
		BackgroundPicture backgroundPicture = new BackgroundPicture();
		backgroundPicture.setCreateDate(new Date());
		backgroundPicture.setPicture(picture);
		backgroundPictureRepository.save(backgroundPicture);
		backLogServiceInterface.addBackLog(uid, "增加修改运营背景图片");
		Result result = new Result();
		result.setKey("success");
		result.setValue("保存成功");
		
		return result;
	}

	@Override
	public Result getAllBackgroundPicture() {
		Result result = new Result();
		result.setKey("success");
		result.setValue(backgroundPictureRepository.findAll());
		return result;
	}

	@Override
	public Result saveManagerInfo(String uid, String phone, String email) {
		managerInfoRepository.deleteAll();
		OperatorManage operatorManage = new OperatorManage();
		operatorManage.setCreateTime(new Date());
		operatorManage.setEmail(email);
		operatorManage.setPhone(phone);
		managerInfoRepository.save(operatorManage);
		backLogServiceInterface.addBackLog(uid, "增加修改管理员信息");
		Result result = new Result();
		result.setKey("success");
		result.setValue("保存成功");
		return result;
	}

	@Override
	public Result getAllManagerInfo() {
		Result result = new Result();
		result.setKey("success");
		result.setValue(managerInfoRepository.findAll());
		return result;
	}

	@Override
	public Result saveFriendShip(String uid, RequestModel requestModel) {
		friendShipRepository.deleteAll();
		List<FriendShip> friendShips = requestModel.getFriendShip();
		List<FriendShip> friends = friendShips.stream().map(e -> new FriendShip(e.getName(),e.getUrl(),new Date())).collect(Collectors.toList());
		backLogServiceInterface.addBackLog(uid, "增加修改友情链接");
		Result result = new Result();
		friendShipRepository.save(friends);
		result.setKey("success");
		result.setValue("保存成功");
		return result;
	}

	@Override
	public Result getAllFriendShip() {
		Result result = new Result();
		result.setKey("success");
		result.setValue(friendShipRepository.findAll());
		return result;
	}

	@Override
	public Map<String, Object> getAllData() {
		Map<String, Object> map = new HashMap<>();
		map.put("backgroundPicture", backgroundPictureRepository.findAll());
		map.put("managerInfo", managerInfoRepository.findAll());
		map.put("friendShip", friendShipRepository.findAll());
		return map;
	}

	
}
